//
//  EditBrokerVC.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/2.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "EditBrokerVC.h"
#import "SelecteStoreVc.h"
#import "AddStoreVc.h"

#import "UserModal.h"
#import "config.h"
#import "BrokerDM.h"
#import "StringTool.h"
#import "MBProgressHUD+MJ.h"
#import "UIView+circular.h"

@interface EditBrokerVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phonetf;
@property (weak, nonatomic) IBOutlet UITextField *brokerNametf;

@property (weak, nonatomic) IBOutlet UIButton *companyBtn;

@property (copy,nonatomic) NSString *storeId;//修改的storeID
@property (nonatomic,strong)BrokerDM *dataManage;

@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;

@end

@implementation EditBrokerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}
-(void)initView
{
    [self.titleView setMianTitle:@"修改信息"];
    self.titleView.doneBtn.hidden = YES;
    
    [self.saveBtn viewToCircular];
    
    if(![StringTool isBlankString:self.model.brokerMobile]){
        self.phonetf.text = self.model.brokerMobile;
    }
    if (![StringTool isBlankString:self.model.storeName]) {
        
        [self.companyBtn setTitle:self.model.storeName forState:UIControlStateNormal];
    }
    if (![StringTool isBlankString:self.model.brokerName]) {
    
        self.brokerNametf.text = self.model.brokerName;
    }

    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    
    self.companyBtn.layer.borderColor = UIColorFromRGB(0xe3e3e3).CGColor;
    self.companyBtn.layer.borderWidth= 1;
    self.companyBtn.layer.cornerRadius = 6;
    [self.companyBtn.layer setMasksToBounds:true];
    
    self.brokerNametf.layer.borderColor = UIColorFromRGB(0xe3e3e3).CGColor;
    self.brokerNametf.layer.borderWidth= 1;
    self.brokerNametf.layer.cornerRadius = 6;
    self.brokerNametf.delegate = self;
    
    
    self.phonetf.layer.borderColor = UIColorFromRGB(0xe3e3e3).CGColor;
    self.phonetf.layer.borderWidth= 1;
    self.phonetf.layer.cornerRadius = 6;
    self.phonetf.delegate = self;
    
    {
        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 1)];
        [self.brokerNametf setLeftViewMode:UITextFieldViewModeAlways];
        [self.brokerNametf setLeftView:leftView];
    }
    
    {
        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 1)];
        [self.phonetf setLeftViewMode:UITextFieldViewModeAlways];
        [self.phonetf setLeftView:leftView];
    }
    
    self.dataManage = [[BrokerDM alloc]init];
}


#pragma mark 点击了按钮
- (IBAction)btnOnClick:(UIButton *)sender {
    
    if (![StringTool isBlankString:self.brokerNametf.text]) {
        
        [MBProgressHUD showMessage:@"请稍等" toView:self.view];
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            ResulteMod *mod = [self.dataManage modifybrokerWithBrokerId:self.model.brokerId WithBrokerPhone:self.model.brokerMobile withbrokerName:self.brokerNametf.text];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                [self.navigationController popViewControllerAnimated:YES];
                
                if (mod.resltState == ResultFail) {
                    [MBProgressHUD showError:mod.resultTip];
                }else if (mod.resltState == ResultNetError)
                {
                    [MBProgressHUD showError:@"网络错误"];
                }else
                {
                    [MBProgressHUD showSuccess:@"修改成功"];
                }
                
            });
            
        });
    }else
    {
        [MBProgressHUD showError:@"请输入经纪人的姓名"];
    }
}


- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    
    CGRect dd =  textField.frame;
    
    int tvbotY = dd.size.height+dd.origin.y;
    int mid  = PMH-[UserModal shareUserModel].keyboardHeight-tvbotY-64;
    
    if (mid<0) {
        
        const float movementDuration = 0.3f;
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        
        CGRect buf = self.mainView.frame;
        buf.origin.y = up ? mid : 0;
        [self.mainView setFrame:buf];
        
        [UIView commitAnimations];
    }else if(up==false){
        
        [self viewRest];
    }
}



-(void)viewRest{
    
    
    const float movementDuration = 0.3f;
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    
    CGRect buf = self.mainView.frame;
    buf.origin.y =  0;
    [self.mainView setFrame:buf];
    
    [UIView commitAnimations];
}

#pragma et delegare
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [self animateTextField:textField up:false];
    
    [textField resignFirstResponder];
    
    [self viewRest];
    return  YES;
}

@end
